new Vue({
    el: "#app",
    data: {
        expandLogin: false, // 默认不展开登录下拉
        orderindex: 0, // 默认排序
        blogCategoryList: [],
        blogList: [],
        cindex: -1,
        loading: 0,  // 0是隐藏 1是数据正在加载中
        flag: false, // 是否到底
        //  关于博客内容的返回和分页信息
        markflag: false,
        cid: 0,// 代表查询全部
        sortnum: 0, // 创建时间排降序 1 升序     2和0 降序
        pageNo: 1,
        total: 0,
        pages: 0,
        loadStat: 0,
        pageSize: 10
    },
    created: function () {
        // 获取cid
        this.cid = document.getElementById("cid").value;

        this.loadBlogCategory()
        this.loadBlog()



    },
    methods: {
        searchOrder(index) {
            this.orderindex = index;
            // 设计排序
            this.sortnum = index;
            this.blogList = [];
            this.pageNo = 1;

            // 查询sql语句
            this.loadBlog()

        },

        // 头部展开和隐藏
        expandLoginBox() {
            this.expandLogin = !this.expandLogin;
        },

        // 根据分类查询博客数据
        searchById (cid, index) {
            this.blogList = [];
            this.pageNo = 1;
            this.cindex = index;
            document.getElementById("cid").value = cid;
            this.cid = cid;
            this.loadBlog();
            console.log(this.cindex+"--"+index)
        },

        // 查询分类信息
        loadBlogCategory () {
            axios.get("/api/category/load").then(res => {
                this.blogCategoryList = res.data;
                var zindex = -1;

                for (var i = 0; i < this.blogCategoryList.length; i++) {
                    if (this.blogCategoryList[i].id === this.cid) {
                        this.cindex = i;
                        break;
                    }
                }


            })
        },

        // 更多信息
        loadMore () {
            // console.log(this.pageNo)
            // if (this.pageNo == this.pages) {
            //     this.flag = true;
            //     alert("没有更多了");
            //     return ;
            // }
            console.log(this.pageNo+"---"+ this.pages)
            this.flag = this.pageNo === (this.pages - 1);
            this.pageNo++;
            this.loadBlog();
            console.log(this.pageNo+"---"+ this.pages)
        },

        async loadBlog () {
            try {
                this.loading = 1;   // 加载数据状态改成1  显示出来
                let res = await axios.get(`/api/blog/load?pageNo=${this.pageNo}&pageSize=${this.pageSize}&cid=${this.cid}&sortnum=${this.sortnum}`);
                // this.blogList = res.data.records; // 覆盖
                this.blogList = this.blogList.concat(res.data.records); // 覆盖
                // 1: 获取后台返回的分页对象数据
                let blogPage = res.data;
                this.total = blogPage.total;
                this.pages = blogPage.pages;

                this.loading = 0;   // 数据加载完毕  隐藏
            } catch (e) {
                console.log("服务器出现异常", e);
            }


        }
    }

});